<template>
  <div>
    <!-- 轮播图部分 -->
    <div id="carousel">
      <el-carousel :interval="4000" type="card" height="200px">
        <el-carousel-item v-for="item in 4" :key="item">
          <img :src="`${$imageUrl}?id=` + 1220" class="carousel-image">
        </el-carousel-item>
      </el-carousel>
    </div>

    <!-- 导师信息部分 -->
    <el-card id="middle" style="width:100%;height:500px;background:;margin:0 auto;position:relative;padding:0;border-radius:25px;">
      <div id="left" style="height:500px;width:430px;background:;float:left;border-radius:25px;margin-left:100px;">
        <p style="text-align:center;font-size:30px;">
          <b>{{ currentTutor.tname }}</b>
        </p>
        <div v-if="currentTutor.imageId" :style="`background-image: url(${getImageUrl(currentTutor.imageId)})`" class="teacher-image"></div>
        <p style="text-align:center;">
          <!-- <b style="width:50%;font-size:30px;display:block;float:left;">数据分析</b>
          <b style="width:50%;font-size:30px;display:block;float:left;">数据剑魔</b> -->
        </p>
        <div style="display:flex;justify-content:center;margin-top:10px;">
          <el-button
            class="carousel-button"
            style="position:absolute;left:0;bottom:45%;"
            type="primary"
            icon="el-icon-arrow-left"
            circle
            size="small"
            @click="showPreviousTutor"
          ></el-button>
          <el-button
            class="carousel-button"
            style="position:absolute;right:0;bottom:45%;"
            type="primary"
            icon="el-icon-arrow-right"
            circle
            size="small"
            @click="showNextTutor"
          ></el-button>
        </div>
      </div>
      <div id="right" style="height:500px;width:390px;background:;float:left;border-radius:25px;margin-left:150px;">
        <p style="text-align:center;font-size:30px;">
          <b>导师简介</b>
        </p>
        <div style="text-indent:2em;font-size:25px;">{{ currentTutor.tintroduction }}</div>
      </div>
    </el-card>

    <!-- 推荐课程部分 -->
    <div id="recommend" style="margin-top: 20px;">
      <h2>推荐课程</h2>
      <div class="course-list">
        <el-card class="course-item" v-for="course in currentTutorCourses.slice(0, 4)" :key="course.id">
          <div class="course-content-container">
            <div class="course-image" :style="`background-image: url(${getImageUrl(course.imageId)})`"></div>
            <div class="course-details">
              <div class="course-title"><h3>{{ course.courseTitle }}</h3></div>
              <div class="course-content">{{ course.courseContent }}</div>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      tutorList: [], // 导师列表
      currentTutorIndex: 0, // 当前导师索引
      recommendedCourses: [], // 推荐课程列表
      currentTutorCourses: [], // 当前导师的课程列表
      $imageUrl: 'http://101.43.223.224:80991/images' // 替换为您的图片 URL
    };
  },
  created() {
    this.fetchTutorData(); // 获取导师数据
  },
  computed: {
    currentTutor() {
      return this.tutorList[this.currentTutorIndex] || {};
    },
  },
  methods: {
    // 获取导师数据的方法
    fetchTutorData() {
      axios.get('/starTeacher/StarTeacherList')
        .then(response => {
          this.tutorList = response.data.data;                              
          this.fetchTutorCourses(this.currentTutor.id);
        })
        .catch(error => {
          console.error('获取导师数据出错:', error);
        });
    },
    // 获取当前导师的课程列表的方法
    fetchTutorCourses(teacherId) {
      axios.get('/starTeacher/StarTeacherCourseList', {
        params: {
          teacherId: teacherId
        }
      })
        .then(response => {
          this.currentTutorCourses = response.data.data;
        })
        .catch(error => {
          console.error('获取导师课程出错:', error);
        });
    },
    // 显示前一个导师的方法
    showPreviousTutor() {
      if (this.currentTutorIndex > 0) {
        this.currentTutorIndex--;
        this.fetchTutorCourses(this.currentTutor.id);
      } else {
        // 当前是第一个导师，切换到最后一个导师
        this.currentTutorIndex = this.tutorList.length - 1;
        this.fetchTutorCourses(this.currentTutor.id);
      }
    },
    // 显示后一个导师的方法
    showNextTutor() {
      if (this.currentTutorIndex < this.tutorList.length - 1) {
        this.currentTutorIndex++;
        this.fetchTutorCourses(this.currentTutor.id);
      } else {
        // 当前是最后一个导师，切换到第一个导师
        this.currentTutorIndex = 0;
        this.fetchTutorCourses(this.currentTutor.id);
      }
    },
    // 获取图片的完整 URL 的方法
    getImageUrl(imageId) {
      return `${this.$imageUrl}?id=${imageId}`;
    },
  },
};
</script>


<style scoped>
/* 轮播图样式 */
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}

.el-carousel__item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-button {
  text-align: center;
  width: 40px;
  height: 40px;
  border-radius: 25px;
  z-index: 1000;
  background-color: rgba(0, 0, 0, 0.2);
  border: none;
}

/* 导师信息样式 */
.teacher-image {
  width: 100%;
  height: 300px;
  background-size: cover;
  background-position: center;
  border-radius: 25px;
}

/* 推荐课程样式 */
.course-list {
  display: flex;
  flex-wrap: wrap;
}

.course-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  margin-right: 10px;
  padding: 10px;
  /* background-color: gray; */
  border-radius: 25px;
  width: 250px;
  height: 250px;
}

.course-image {
  width: 220px;
  height: 120px;
  background-size: cover;
  margin-left: -5px;


}

.course-details {
 
  display: flex;
  flex-direction: column;
}

.course-title {
  width: 270px;

  margin-top: 10px;
}

.course-content {
  flex-grow: 1;

}
</style>
